package efg.library.EJB;

import java.util.HashSet;

import javax.annotation.security.DeclareRoles;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;

import org.jboss.ejb3.annotation.SecurityDomain;
import org.jboss.wsf.spi.annotation.WebContext;

import efg.library.IF.BookDAO;
import efg.library.IF.CustomerDAO;
import efg.library.IF.LibraryManagerIF;

@Stateless
@WebService(name = "LibraryManager", serviceName = "LibraryManagerService", targetNamespace = "librarymanager")
@WebContext(authMethod="BASIC")
@SecurityDomain(value = "ABC-Library-Security-Domain", unauthenticatedPrincipal = "anonymous")
@DeclareRoles({"manager", "administrator", "cashier"})

public class LibraryManagerBean implements LibraryManagerIF
{
	public LibraryManagerBean()
	{
		System.out.println("LibraryManager()");
	}

	@WebMethod
	@RolesAllowed({"cashier","administrator","manager"})	
	public HashSet<BookDAO> allBook()
	{
		
		HashSet<Book> books = Book.allBooks();


		HashSet<BookDAO> booksdao = new HashSet<BookDAO>();
		for(Book b : books) {
			BookDAO bd = new BookDAO();
			bd.setTitle(b.getTitle());
			if(b.getLendTo() != null) {
				bd.setLendTo(b.getLendTo().getName());
			}
			booksdao.add(bd);
		}
		

		return booksdao;

		
	}

	
	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public HashSet<String> allBooks(String name)
	throws Exception
	{
		Customer customer = Customer.findCustomer(name);
		if (customer != null)
		{
			HashSet<Book> books = customer.getBooks();

			HashSet<String> bookss = new HashSet<String>();
			for(Book b : books) {
				bookss.add(b.getTitle());
			}

			return bookss;
		}
		throw new Exception("\""+name+"\" does not exist in database"); 
	}

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public HashSet<CustomerDAO> allCustomers()
	{
		HashSet<Customer> customers = Customer.allCustomers();

		HashSet<CustomerDAO> customerdao = new HashSet<CustomerDAO>();
		for(Customer c : customers) {
			CustomerDAO cd = new CustomerDAO();
			cd.setName(c.getName());
			cd.setCredits(c.getCredits());
			HashSet<String> books = new HashSet<String>();

			for (Book b: c.getBooks())
			{
				books.add(b.getTitle());

			}
			cd.setBooks(books);

			customerdao.add(cd);
		}

		return customerdao;
	} 

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public String addCredits(String name, int credits) 
	throws Exception
	{
		System.out.println("LibraryManager.addCredits("+name+", "+credits+")");   
		Customer customer = Customer.findCustomer(name);
		if (customer != null)
		{
			int newCredits = credits+customer.getCredits();
			customer.setCredits(newCredits);
			return "\""+name+"\" has "+newCredits+" credits";
		}
		throw new Exception("\""+name+"\" not exist in database");  
	}

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public String lend(String title, String name)
	throws Exception
	{
		System.out.println("LibraryManager.lend("+title+", "+name+")");
		Book book = Book.findBook(title);
		if (book != null)
		{
			Customer lendTo = book.getLendTo();       
			if (lendTo == null)
			{
				Customer customer = Customer.findCustomer(name); 
				if (customer != null)
				{
					int credits = customer.getCredits();
					if (credits > 0)
					{
						customer.setCredits(--credits);
						customer.addBook(book);
						book.setLendTo(customer);
						return "\""+title+"\" lend to "+name;
					}
					throw new Exception("\""+name+"\" has no credits");
				}
				throw new Exception("\""+name+"\" not exist in database");
			}
			throw new Exception("\""+title+"\" is lend to "+lendTo.getName());
		}
		throw new Exception("\""+title+"\" not exist in database");   
	}

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public String newBook(String title)
	throws Exception
	{
		System.out.println("LibraryManager.newBook("+title+")");   
		Book book = Book.findBook(title);
		if (book == null)
		{
			new Book(title);
			return "\""+title+"\" put in database";
		}
		throw new Exception("\""+title+"\" exist in database");     
	}

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public String newCustomer(String name)
	throws Exception
	{
		System.out.println("LibraryManager.newCustomer("+name+")");    
		Customer customer = Customer.findCustomer(name);
		if (customer == null)
		{
			new Customer(name);
			return "\""+name+"\" put in database";
		}
		throw new Exception("\""+name+"\" exist in database");     
	}

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public String removeBook(String title)
	throws Exception
	{
		System.out.println("LibraryManager.removeBook("+title+")");   
		Book book = Book.findBook(title);
		if (book != null)
		{
			Book.removeBook(title);
			return "\""+title+"\" removed from database";
		}
		throw new Exception("\""+title+"\" not exist in database");     
	}

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public String removeCustomer(String name)
	throws Exception
	{
		System.out.println("LibraryManager.removeCustomer("+name+")");  
		Customer customer = Customer.findCustomer(name);
		if (customer != null)
		{
			Customer.removeCustomer(name);
			return "\""+name+"\" removed from database";
		}
		throw new Exception("\""+name+"\" not exist in database");     
	} 

	@RolesAllowed({"cashier","administrator","manager"})
	@WebMethod
	public String unlend(String title)
	throws Exception
	{
		System.out.println("LibraryManager.unlend("+title+")");  
		Book book = Book.findBook(title);   
		if (book != null)
		{
			Customer customer = book.getLendTo();
			if (customer != null)
			{
				book.setLendTo(null);
				customer.removeBook(book);
				return "\""+title+"\" is free";
			}
			throw new Exception("\""+title+"\" was free");
		}
		throw new Exception("\""+title+"\" not exist in database");
	}
}
